<?php

namespace App\Listeners;

use App\Jobs\SetAdminLog;
use Carbon\Carbon;
use Illuminate\Foundation\Bus\DispatchesJobs;


class UserEventListener
{
    use DispatchesJobs;
    /**
     * Handle user login events.
     */
    public function onUserLogin($user, $remember) {
        //判断是否为后台登陆
        if ($user->type != 'system')
            return false;
        $geoIP = geoip()->getLocation();
        //push to queue
        $this->dispatch(new SetAdminLog($user->id, $geoIP, 'Login', Carbon::now(),'Login'));
    }

    /**
     * Handle user logout events.
     */
    public function onUserLogout($user) {
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'auth.login',
            'App\Listeners\UserEventListener@onUserLogin'
        );

        $events->listen(
            'auth.logout',
            'App\Listeners\UserEventListener@onUserLogout'
        );
    }
}
